<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * This class is just for TripInformation CMS
 */
class Admin extends BackendController {
	
	public $data = array();
	public $template_url = '';
	public $template_url_frontend = '';
	public function __construct() {
		parent::__construct();
		
		//Initialize variable
		$this->template_url = $this->data['template_url'] = base_url('public/backend').'/';
		$this->template_url_frontend = $this->data['template_url_frontend'] = base_url('public/').'/';
		
		//Load library
		$this->load->helper('common');
		$this->load->helper(array('form', 'url'));
		
		//Config library image
		$config['upload_path'] = BASE_DIR.'/public/images/upload/';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['encrypt_name'] = FALSE;
		$this->load->library('upload', $config);

		//Load database
		$arr_load = array('info_model','video_model','protfolio_model','image_model');
		$this->load->model($arr_load);	
		
		//Get session data
		$this->data['ss_username'] = $this->session->userdata('username');

		//Load resize lib
		$this->load->library('image_lib');
	}
	
	//Show all menu
	public function index(){
			//Load view			
			$this->load->view('backend/index',$this->data);		
	}
	
	//Video function
	public function video(){
		//Load all data
		$data = $this->video_model->get();
		$this->data['data'] = $data;		
		$this->load->view('backend/video',$this->data);
	}

	//Edit info function
	public function edit_info(){
			//Submit form
			if( isset($_POST['update']) ){
				
				$this->data['error'] = '';
				//Update exist images to server
				$arr_update = array();
				if( isset($_FILES[ 'file']['name']) && $_FILES[ 'file']['name'] ){
					$rs_upload = $this->module_upload_image('file');
					$arr_update_image[] = $rs_upload;
					if( !$rs_upload['error'] ){
						$arr_update[] = array('image_url'=>$rs_upload['file_name']);
					}
					else{
						$this->data['error'] = $rs_upload['error'];
						break;
					}
				}
				
				//Update, insert table image, table render
				if( !$this->data['error'] ){
					//Update info table
					if( isset($arr_update[0]) && $arr_update[0] ){
						$arr_update = array('name'=>$_POST['name'],
											'email'=>$_POST['email'],
											'description'=>$_POST['description'],
											'image_url'=>$arr_update[0]['image_url']);
					}
					else{
						$arr_update = array('name'=>$_POST['name'],
											'email'=>$_POST['email'],
											'description'=>$_POST['description']);
					}
					$where = array( 'id'=>$_GET['id'] );
					$this->info_model->update( $arr_update,$where );
					//Notification
					$this->data['error'] = 0;					
				}
			}
			
			//Get data from database
			$where = array('id'=>$_GET['id']);
			$rs_get = $this->info_model->get($where);
			$this->data['data'] = $rs_get[0];
			unset($_POST);
			unset($_FILES);
			//printr($rs_get);
			
			//Load view			
			$this->load->view('backend/edit_info',$this->data);			
	}
	
	//Add video function
	public function add_video(){
		//Submit form
		if( isset($_POST['update']) ){
			
			//Insert render table
			$arr_insert = array('name'=>$_POST['name'],
								'video_url'=>$_POST['video_url'],);
			$id_insert = $this->video_model->insert($arr_insert);					
			
			//Notification
			$this->data['error'] = 0; 
		}
		//Load view			
		$this->load->view('backend/add_video',$this->data);			
	}

	//Edit animation function
	public function edit_video(){
		if( isset($_GET['id']) && $_GET['id'] ){
			//Submit form
			if( isset($_POST['update']) ){
				//Update render table
				$where = array( 'id'=>$_GET['id'] );
				$arr_update = array('name'=>$_POST['name'],
									'video_url'=>$_POST['video_url'],);
				$this->video_model->update($arr_update,$where);					
				//Notification
				$this->data['error'] = 0; 
			}
			
			//Load all data
			$data = $this->video_model->get( array('id'=>$_GET['id']) );
			$this->data['data'] = $data[0];
			
			//Load view			
			$this->load->view('backend/edit_video',$this->data);			
		}
	}	

	//Ajax delete video function
	public function ajax_delete_video(){
		if( isset($_POST['id']) && $_POST['id']){
				//Delete image in render,image table
				$where = array( 'id'=>$_POST['id'] );
				$this->video_model->delete( $where );
				
				//Return result
				echo json_encode( array('status'=>'ok') );
				exit;
			}
		//Return result
		echo json_encode( array('status'=>'error') );
		exit;
	}
	
	//Ajax delete protfolio function
	public function ajax_delete_protfolio(){
		if( isset($_POST['id']) && $_POST['id']){
			//Delete image in render,image table
			$where = array( 'id'=>$_POST['id'] );
			$this->protfolio_model->delete( $where );
			
			//Delete image of menu
			$where = array( 'fk_menu_id'=>$_POST['id'] );
			$rs_image = $this->image_model->get($where);
			$arr_id = '';
			if( $rs_image ){
				foreach($rs_image as $item){
					$arr_id[] = $item['id'];
				}
				$this->module_delete_image( $arr_id );				
			}
	
			//Return result
			echo json_encode( array('status'=>'ok') );
			exit;
		}
		//Return result
		echo json_encode( array('status'=>'error') );
		exit;
	}	
	
	//Ajax delete image function
	public function ajax_delete_image(){
		if( isset($_POST['id']) && $_POST['id']){
			//Delete image in server
	
			$this->module_delete_image( array($_POST['id']) );
	
			//Delete image in database
			$where = array( 'id'=>$_POST['id'] );
			$this->image_model->delete( $where );
	
			//Return result
			echo json_encode( array('status'=>'ok') );
			exit;
		}
		//Return result
		echo json_encode( array('status'=>'error') );
		exit;
	}	

	//Protfolio function
	public function protfolio(){
		//Load all data
		$data = $this->protfolio_model->get( array('parent_id'=>0) );
		$this->data['data'] = $data;
		$this->load->view('backend/protfolio',$this->data);
	}	
	
	//Edit protfolio function
	public function edit_protfolio(){
		if( isset($_GET['id']) && $_GET['id'] ){
			//Submit form
			if( isset($_POST['update']) ){
				//Update render table
				$where = array( 'id'=>$_GET['id'] );
				$arr_update = array('name'=>$_POST['name']);
				$this->protfolio_model->update($arr_update,$where);
				//Notification
				$this->data['error'] = 0;
			}
				
			//Load all data
			$data = $this->protfolio_model->get( array('id'=>$_GET['id']) );
			$this->data['data'] = $data[0];
				
			//Load view
			$this->load->view('backend/edit_protfolio',$this->data);
		}
	}
	
	//Edit list image of protfolio
	public function edit_protfolio_(){
		if( isset($_GET['id']) && $_GET['id'] ){
			//Submit form
			if( isset($_POST['update']) ){
				/****Insert new image****/
				$this->data['error'] = 0;
				for( $i=0; $i<$_POST['num_file']; $i++ ){
					if( $_FILES['newfile'.$i]['name'] ){
						$rs_upload = $this->module_upload_image('newfile'.$i);
						if( !$rs_upload['error'] ){
							$arr_insert[] = array('image_url'=>$rs_upload['file_name'],
												  'fk_menu_id'=>$_GET['id']);
						}
						else{
							$this->data['error'] = $rs_upload['error'];
							break;
						}
					}
				}
	
				/***********Update exist image**********/
				if( $this->data['error'] === 0 ){
					//Count num image of render
					$where = array('fk_menu_id'=>$_GET['id']);
					$rs_get = $this->image_model->get($where);
						
					//Update exist images to server
					$arr_update = array();
					if($rs_get){
						foreach ($rs_get as $item){
							if( isset($_FILES[ 'file'.$item['id'] ]['name']) && $_FILES[ 'file'.$item['id'] ]['name'] ){
								$rs_upload = $this->module_upload_image('file'.$item['id']);
								$arr_update_image[] = $rs_upload;
								if( !$rs_upload['error'] ){
									$arr_update[] = array('image_url'=>$rs_upload['file_name']);
									$where[] = array( 'id'=>$item['id'] );
								}
								else{
									$this->data['error'] = $rs_upload['error'];
									break;
								}
							}
						}
					}
						
					//Update, insert table image, table render
					if( !$this->data['error'] ){
						//Update image
						if( isset($arr_update[0]) ){
							for( $i=0; $i<count($arr_update); $i++ ){
								//Delete old image
								$this->module_delete_image( array('id'=>$where[$i]['id']) );
								//Update database
								$this->image_model->update($arr_update[$i],$where[$i]);
							}
						}
						//Insert new image
						if( isset($arr_insert[0]) ){
							for( $i=0; $i<count($arr_insert); $i++ ){
								$rs = $this->image_model->insert( $arr_insert[$i] );}
						}
					}
				}
				/**********End update exist image********/
			}
				
			//Get data from database
			$where_menu = array('id'=>$_GET['id']);
			$where_image = array('fk_menu_id'=>$_GET['id']);
			
			$this->data['protfolio'] = $this->protfolio_model->get($where_menu);
			$this->data['image'] = $this->image_model->get($where_image);
			unset($_POST);
			unset($_FILES);
				
			//Load view
			$this->load->view('backend/edit_protfolio_',$this->data);
		}
	}	

	//Add Protfolio function
	public function add_protfolio(){
		if( isset($_POST['update']) ){
			if( $_POST['name'] && $_POST['parent_menu'] ){
				$arr_insert = array( 'parent_id'=>$_POST['parent_menu'], 
									 'name'=>$_POST['name'] );
				$this->protfolio_model->insert( $arr_insert );
				$this->data['error'] = 0;
			}
		}
		//Load all data
		$parent_menu = $this->protfolio_model->get( array('parent_id'=>0) );
		$this->data['parent_menu'] = $parent_menu;
		$this->load->view('backend/add_protfolio',$this->data);
	}


	//List Protfolio function
	public function list_protfolio(){
		if( isset($_GET['id']) && $_GET['id'] ){
			//Load all data
			$this->data['data'] = $this->protfolio_model->get( array('parent_id'=>$_GET['id']) );
			//Load view
			$this->load->view('backend/list_protfolio',$this->data);
		}
	}

	
	//Module upload image
	//Input: Image_name
	//@Description: upload image base in image_name in $_FILES
	public function module_upload_image($image_name){
		if( $image_name ){
			if ( ! $this->upload->do_upload($image_name) ){
				//Upload failed
				$error = $this->upload->display_errors();
				return array('error' => $error,
						'file_name'=>'');
			}
			else{
				//Upload success
				$data = $this->upload->data();
				$this->module_resize_image($data['file_name']);
				return array('file_name'=>$data['file_name'],
						'error'=>'');
			}
		}
		return false;
	}	
	
	//Module resize image, and create thumbnail
	//Input: Image_name
	public function module_resize_image($image_name){
		$image_url = BASE_DIR.'/public/images/upload/';
		if( $image_name ){
			//Resize image
			$config_resize = array();
			$config_resize['image_library'] = 'gd2';
			$config_resize['source_image']	= BASE_DIR.'/public/images/upload/'.$image_name;
			//$config_resize['new_image']	= BASE_DIR.'/public/images/upload/resized/';
			$config_resize['maintain_ratio'] = TRUE;
			$config_resize['width']	 = WIDTH;
			$config_resize['height'] = HEIGHT;
			$config_resize['create_thumb']	= FALSE;
			$this->image_lib->initialize($config_resize);
			if( !$this->image_lib->resize() ){
				 echo $this->image_lib->display_errors();exit;
			}
			$this->image_lib->clear();
			
 			//Create thumbnail
			$config_resize = array();
			$config_resize['image_library'] = 'gd2';
			$config_resize['source_image']	= BASE_DIR.'/public/images/upload/'.$image_name;
			//$config_resize['new_image']	= BASE_DIR.'/public/images/upload/thumb/';
			$config_resize['maintain_ratio'] = TRUE;
			$config_resize['width']	 = WIDTH_THUMB;
			$config_resize['height'] = HEIGHT_THUMB;
			$config_resize['create_thumb']	= TRUE;
			$config_resize['thumb_marker']	= '_thumb';
			$this->image_lib->initialize($config_resize);
			if( !$this->image_lib->resize() ){
				 echo $this->image_lib->display_errors();exit;
			}	
			$this->image_lib->clear();		
		}
		return false;
	}

	//Module delete image
	//Input: array image_id
	public function module_delete_image($image_id){
		if( is_array($image_id) ){
			foreach ($image_id as $item){
				//Remove link in server
				$rs_image = $this->image_model->get( array('id'=>$item) );
				$image_link = BASE_DIR.'/public/images/upload/'.$rs_image[0]['image_url'];
				$thumbnail_name = get_thumb($rs_image[0]['image_url']);
				$thumbnail_link = BASE_DIR.'/public/images/upload/'.$thumbnail_name;
				
				//Delete image if exist
				if( file_exists($image_link) )
					@unlink($image_link);
					
				//Delete thumbnail if exist
				if(file_exists($thumbnail_link))
					@unlink($thumbnail_link);
			}
		}
	}	
	
	public function delete(){
		$filename = BASE_DIR.'/public/images/girl_thumb.jpg';
		if(file_exists($filename))
			$rs = unlink($filename);
	}
}

